package com.baseproject.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * Created by hp on 2017/5/24.
 */

public class ByteUtil {
    private static final String TAG = "ByteUtil";
    private static final String accessKey = "0f30f5bfe9f842a47bd7b3b9f1f8e85b";

    /**
     * 生成post请求的签名
     *
     * @param params 参数集合
     * @return 签名
     */
    public static Map<String, String> generatePostSign(Map<String, String> params) {
        params.put("rstr", getRandomString(32));
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        Set<String> keySet = params.keySet();
        Object[] keys = keySet.toArray();
        Arrays.sort(keys);
        String keyValueString = "";
        for (int i = 0; i < keys.length; i++) {
            if (!TextUtils.isEmpty(params.get(keys[i]))) {
                keyValueString += (keys[i] + "=" + params.get(keys[i]) + "&");
            }
        }
        keyValueString += ("key=" + accessKey);
        Log.i(TAG, "keyValueString is: " + keyValueString);
        String sign = MD5Util.encode(keyValueString);
        Log.i(TAG, "加密后的sign为 : " + sign);
        // params.put("sign", sign);
        return params;
    }

    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getUmengChannel(Context context) {
        ApplicationInfo ai = null;
        try {
            ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (ai == null) {
                return null;
            }
            String channel = ai.metaData.getString("UMENG_CHANNEL", "");
            if (!"".equals(channel)) {
                return channel;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            return ai.metaData.getString("UMENG_CHANNEL");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getClientVersion(Context context) {
        try {
            PackageInfo pkg = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pkg.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getClientVersionCode(Context context) {
        try {
            PackageInfo pkg = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pkg.versionCode;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
